package com.cw1.principle.inversion;

/**
 * 依赖倒转原则 --cw
 */
public class DependencyPassCw {
    public static void main(String[] args) {
        CwTv1 cwTv1 = new CwTv1();
        Open1 open1 = new Open1();
        open1.open(cwTv1);
        System.out.println("===================");

        CwTv2 cwTv2 = new CwTv2();
        Open2 open2 = new Open2(cwTv2);
        open2.open();
        System.out.println("===================");

        CwTv3 cwTv3 = new CwTv3();
        Open3 open3 = new Open3();
        open3.setTv(cwTv3);
        open3.open();
    }
}
/**
 * 方式一：通过接口调用来实现
 */
interface TV1{
    void play();
}
interface IOpen1{
    void open(TV1 tv1);
}
class Open1 implements IOpen1{
    public void open(TV1 tv1) {
        tv1.play();
    }
}
class CwTv1 implements TV1{
    public void play() {
        System.out.println("CwTv1打开了：流批1");
    }
}

/**
 * 方式二：通过构造器来实现
 * 通过构造方法来进行赋值
 */
interface TV2{
    void play();
}
interface IOpen2{
    void open();
}
class Open2 implements IOpen2{
    private TV2 tv2;
    public Open2(TV2 tv2) {
        this.tv2 = tv2;
    }
    public void open() {
        tv2.play();
    }
}
class CwTv2 implements TV2{
    public void play() {
        System.out.println("CwTv2打开了：流批2");
    }
}


/**
 * 方式三：通过setter方法来实现
 * 通过setter方法来进行赋值
 */
interface TV3{
    void play();
}
interface IOpen3{
    void open();
    void setTv(TV3 tv3);
}
class Open3 implements IOpen3{
    private TV3 tv3;
    public void open() {
        this.tv3.play();
    }
    public void setTv(TV3 tv3) {
        this.tv3 = tv3;
    }
}
class CwTv3 implements TV3{
    public void play() {
        System.out.println("CwTv3打开了：流批3");
    }
}
